<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1,
			shrink-to-fit=no" name="viewport">
    <title>书友网首页</title>
    <link rel="stylesheet" href="./assets/css/app.min.css">
    <link rel="stylesheet" href="./assets/css/style.css">
    <link rel="stylesheet" href="./assets/css/components.css">
    <link rel="stylesheet" href="./assets/css/custom.css">
    <link rel='shortcut icon' type='image/x-icon' href='./assets/img/favicon.ico'/>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
        .el-tag + .el-tag {
            margin-left: 10px;
        }
        .button-new-tag {
            margin-left: 10px;
            height: 32px;
            line-height: 30px;
            padding-top: 0;
            padding-bottom: 0;
        }
        .input-new-tag {
            width: 90px;
            margin-left: 10px;
            vertical-align: bottom;
        }
    </style>
    <style>
        [v-cloak]{
            display: none;
        }
    </style>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 80px;
            height: 80px;
            line-height: 80px;
            text-align: center;
        }
        .avatar {
            width: 80px;
            height: 80px;
            display: block;
        }
    </style>
</head>

<body>
<div id="indexVue" v-cloak>
    <!--<div class="loader"></div>-->
    <div id="app">

        <div class="main-wrapper main-wrapper-1">
            <div class="navbar-bg"></div>

            <user-cart :avatar="user.avatar" :account="user.account"></user-cart>
            <!-- 左侧菜单页 -->
            <div class="main-sidebar sidebar-style-2">
                <aside id="sidebar-wrapper">
                    <div class="sidebar-brand">
                        <a href="Index.html">
                            <img alt="image" src="./assets/img/logo.png" class="header-logo"/>
                            <span class="logo-name">书友网</span>
                        </a>
                    </div>
                    <div class="sidebar-user">
                        <div class="sidebar-user-picture">
                            <img alt="image" :src="user.avatar">
                        </div>
                        <div class="sidebar-user-details">
                            <div class="user-name">{{user.nickName}}</div>
                            <div class="user-role">{{user.account}}</div>
                        </div>
                    </div>
                    <el-menu background-color="#fff" text-color="#000" active-text-color="#409EFF" unique-opened
                             :collapse="isCollapse"
                             :collapse-transition="false"
                             :default-active="activePath">
                        <!-- 一级菜单 -->
                        <template  v-for="item in menuList">
                            <el-submenu v-if="item.children.length > 0" :index="item.id + ''" :key="item.id">
                                <!-- 一级菜单的模板区域 -->
                                <template slot="title">
                                    <i :class="item.icon"></i>
                                    <span>{{item.menuName}}</span>
                                </template>

                                <!-- 二级菜单 -->
                                <el-menu-item v-if="item.children" :index="subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(subItem.path)">
                                    <template slot="title">
                                        <span>{{subItem.menuName}}</span>
                                    </template>
                                </el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else  :index="item.path" :key="item.id" @click="saveNavState(item.path)">
                                <!-- 一级菜单的模板区域 -->
                                <template slot="title">
                                    <i :class="item.icon"></i>
                                    <span>{{item.menuName}}</span>
                                </template>
                            </el-menu-item>
                        </template>

                    </el-menu>
                </aside>
            </div>
            <!-- Main Content -->
            <div class="main-content">
                <h2 class="section-title">个人基本信息页面</h2>
                <p class="section-lead">
                    这里输入的东西都是对外公布的、允许其他人查看.
                </p>
                <div class="row">
                    <div class="col-12 col-md-6 col-lg-6">
                        <div class="card card-primary">
                            <div class="card-header">
                                <h4>我的基本信息</h4>
                                <div class="card-header-action">
                                    <a href="#" class="btn btn-primary">
                                       我的卡片
                                    </a>
                                </div>
                            </div>
                            <div class="card-body">
                                <el-form ref="form" :model="userInfo" label-position="left" label-width="80px">
                                    <el-form-item label="个人头像">
                                        <div class="demo-basic--circle">
                                            <el-upload
                                                    class="avatar-uploader"
                                                    action="/file/upload"
                                                    :show-file-list="false"
                                                    :on-success="handleAvatarSuccess"
                                                    :before-upload="beforeAvatarUpload">
                                                <img v-if="userInfo.avatar" :src="userInfo.avatar" class="avatar">
                                            </el-upload>
                                        </div>
                                    </el-form-item>
                                    <el-form-item label="登录账户">
                                        <el-input  :disabled="true" v-model="userInfo.account"></el-input>
                                    </el-form-item>
                                    <el-form-item label="个人邮箱">
                                        <el-input v-model="userInfo.email"></el-input>
                                    </el-form-item>
                                    <el-form-item label="昵称">
                                        <el-input v-model="userInfo.nickName"></el-input>
                                    </el-form-item>
                                    <el-form-item label="性别">
                                        <el-select v-model="userInfo.gender" placeholder="请选择你的性别">
                                            <el-option label="男" :value=1></el-option>
                                            <el-option label="女" :value=2></el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="出生日期">
                                        <el-date-picker
                                                v-model="userInfo.birthday"
                                                type="date"
                                                placeholder="选择出生日期">
                                        </el-date-picker>
                                    </el-form-item>
                                    <el-form-item label="您的身份" prop="resource">
                                        <el-radio-group v-model="userInfo.type">
                                            <el-radio :label="1">中小学生</el-radio>
                                            <el-radio :label="2">大学生</el-radio>
                                            <el-radio :label="3">上班族</el-radio>
                                            <el-radio :label="4">自营</el-radio>
                                        </el-radio-group>
                                    </el-form-item>
                                    <el-form-item label="个人签名">
                                        <el-input type="textarea" v-model="userInfo.remark"></el-input>
                                    </el-form-item>
                                    <el-form-item>
                                        <el-button type="primary" @click="onSubmit">立即更新</el-button>
                                    </el-form-item>
                                </el-form>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-md-6 col-lg-6">
                        <!--<div class="card card-secondary">
                            <div class="card-header">
                                <h4>我的照片</h4>
                                <div class="card-header-action">
                                    <a href="#" class="btn btn-primary">View All</a>
                                    <a href="#" class="btn btn-danger">Delete All</a>
                                </div>
                            </div>
                            <div class="card-body">
                                <el-upload
                                        action="#"
                                        list-type="picture-card"
                                        :auto-upload="false">
                                    <i slot="default" class="el-icon-plus"></i>
                                    <div slot="file" slot-scope="{file}">
                                        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" >
                                        <span class="el-upload-list__item-actions">
                                        <span
                                                class="el-upload-list__item-preview"
                                                @click="handlePictureCardPreview(file)"
                                        >
                                          <i class="el-icon-zoom-in"></i>
                                        </span>
                                        <span
                                                v-if="!disabled"
                                                class="el-upload-list__item-delete"
                                                @click="handleDownload(file)"
                                        >
                                          <i class="el-icon-download"></i>
                                        </span>
                                        <span
                                                v-if="!disabled"
                                                class="el-upload-list__item-delete"
                                                @click="handleRemove(file)"
                                        >
                                          <i class="el-icon-delete"></i>
                                        </span>
                                      </span>
                                    </div>
                                </el-upload>
                            </div>
                        </div>-->
                        <div class="card card-secondary">
                                <div class="card-header">
                                    <h4>个人标签</h4>
                                    <div class="card-header-action">
                                        <a href="#" class="btn btn-primary">展示你的个人魅力</a>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <el-tag
                                            style="margin-top: 8px;"
                                            :key="tag"
                                            v-for="tag in dynamicTags"
                                            closable
                                            :disable-transitions="false"
                                            @close="handleClose(tag)">
                                        {{tag}}
                                    </el-tag>
                                    <el-input
                                            class="input-new-tag"
                                            v-if="inputVisible"
                                            v-model="inputValue"
                                            ref="saveTagInput"
                                            size="small"
                                            @keyup.enter.native="handleInputConfirm"
                                            @blur="handleInputConfirm"
                                    >
                                    </el-input>
                                    <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
                                </div>
                            </div>
                    </div>

                </div>

            </div>
            <my-footer></my-footer>
        </div>
    </div>
</div>
<el-dialog :visible.sync="dialogVisible">
    <img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>

<script src="./common/js/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script src="./compont/foot.js"></script>
<script src="./compont/UserCart.js"></script>
<script src="./common/js/common.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layer/2.3/layer.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

<!-- General JS Scripts -->
<script src="./assets/js/app.min.js"></script>
<!-- JS Libraies -->
<script src="./assets/js/page/index2.js"></script>
<script src="./assets/js/page/todo.js"></script>
<!-- Template JS File -->
<script src="./assets/js/scripts.js"></script>
<!-- Custom JS File -->
<script src="./assets/js/custom.js"></script>



<script>
    const vm = new Vue({
        el: "#indexVue",
        data: {
            user :{
                avatar : '',
                nickName:'',
                userId:'',
                account:'',
            },
            userInfo:{},
            activeIndex2: '1',
            // 左侧菜单数据
            menuList: [],
            // 是否折叠
            isCollapse: false,
            // 被激活的链接地址
            activePath: '',
            dynamicTags: [],
            inputVisible: false,
            inputValue: '',
            dialogImageUrl: '',
            dialogVisible: false,
            disabled: false
        },
        created() {
            this.getMenuList()
            this.getUserInfoList()
            this.getUserInfo()
            this.getTag()
            this.activePath = window.localStorage.getItem('activePath')
        },
        methods:{
            getUserInfoList(){
                var userId = localStorage.getItem("userId");
                var account = localStorage.getItem("account");
                var nickName = localStorage.getItem("nickName");
                var avatar = localStorage.getItem("avatar");
                this.user.account = account;
                this.user.userId = userId;
                this.user.nickName = nickName;
                this.user.avatar = avatar;
            },
            getTag(){
                const that = this;
                HttpGet("/tags/getTag",null,function (res) {
                    if (res.result == true){
                        that.dynamicTags = res.data;
                    }
                })
            },
            handleAvatarSuccess(res, file) {
                if (res.result == true){
                    this.userInfo.avatar = URL.createObjectURL(file.raw);
                    localStorage.setItem("avatar",this.userInfo.avatar);
                    layer.msg("更新头像成功",{icon:1});
                }else{
                    layer.msg("更新头像失败",{icon:2});
                }
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    layer.msg('上传头像图片只能是 JPG 格式!',{icon:2,time:2000});
                }
                if (!isLt2M) {
                    layer.msg('上传头像图片大小不能超过 2MB!',{icon:2,time:2000});
                }
                return isJPG && isLt2M;
            },

            getUserInfo(){
                const that = this;
                HttpGet("/user/getInfo",null,function (res) {
                    if (res.result == true){
                        that.userInfo = res.data;
                    }else{
                        layer.msg(res.message,{icon:2,time:2000},function () {
                            window.location.href = 'Login.html';
                        })
                    }

                })
            },
            handleRemove(file) {
                console.log(file);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleDownload(file) {
                console.log(file);
            },
            handleClose(tag) {
                var data = {
                    tagName : tag
                }
                HttpPost("/tags/remove",data,function (res) {
                    if (res.result == true){
                        layer.msg("删除"+tag+"成功",{icon:2});
                        this.getTag();
                    }else{
                        layer.msg(res.message,{icon:2});
                    }
                })
                console.log(tag);
                this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
            },

            showInput() {
                this.inputVisible = true;
                this.$nextTick(_ => {
                    this.$refs.saveTagInput.$refs.input.focus();
                });
            },

            handleInputConfirm() {
                let inputValue = this.inputValue;
                const that = this;
                if (inputValue) {
                    var data = {
                        tagName : inputValue
                    }
                    HttpPost("/tags/save",data,function (res) {
                        if (res.result == true){
                            that.dynamicTags.push(inputValue);
                            that.inputVisible = false;
                            that.inputValue = '';
                        }else{
                            layer.msg(res.message,{icon:2});
                        }
                    })

                }

            },
            onSubmit() {
                const that = this;
                if (!EmailCheck(that.userInfo.email)){
                    layer.msg("邮箱格式有误！",{icon:2,time:2000})
                    return false;
                }
                HttpPost("/user/updateUser",that.userInfo,function (res) {
                    if (res.result == true){
                        that.userInfo = res.data;
                        localStorage.setItem("account",res.data.account);
                        localStorage.setItem("userId",res.data.userId);
                        localStorage.setItem("nickName",res.data.nickName);
                        localStorage.setItem("avatar",res.data.avatar);
                        layer.msg("更新信息成功",{icon:1,time:2000},function () {
                            window.location.reload();
                        })
                    }else{
                        layer.msg("更新信息失败！请刷新重试~",{icon:2,time:2000})
                    }
                })
            },
            getMenuList(){
                const that = this;
                HttpGet("/menu/getList",null,function (res) {
                    if (res.result == true){
                        that.menuList = res.data;
                    }else{
                        layer.msg("获取列表失败！请刷新重试~",{icon:2,time:2000})
                    }
                })
            },


            // 点击按钮，切换菜单的折叠与展开
            toggleCollapse() {
                this.isCollapse = !this.isCollapse
            },
            // 保存链接的激活状态
            saveNavState(activePath) {
                window.localStorage.setItem('activePath', activePath)
                this.activePath = activePath
                window.location.href = activePath;
            }


        },
    })
</script>
</body>

</html>
